﻿using System;
using UnityEngine;
/// <summary>
/// MonoBehaviour扩展
/// </summary>
public static class SFMonoBehaviourExtendEngine
{
    /// <summary>
    /// 立即销毁MonoBehaviour
    /// </summary>
    /// <param name="_mono">MonoBehaviour</param>
    public static void ImmediateDestory(this MonoBehaviour _mono)
    {
#if UNITY_EDITORs
        GameObject.DestroyImmediate(_mono);
#else
        GameObject.Destroy(_mono);
#endif
    }

    /// <summary>
    /// 获得或添加组件
    /// </summary>
    /// <typeparam name="T">组件类型</typeparam>
    /// <param name="_trans">Transform</param>
    /// <returns>组件</returns>
    public static T GetOrAddComponent<T>(this Transform _trans)
        where T : Component
    {
        return _trans.gameObject.GetOrAddComponent<T>();
    }

    /// <summary>
    /// 获得或添加组件
    /// </summary>
    /// <typeparam name="T">组件类型</typeparam>
    /// <param name="_go">GameObject</param>
    /// <returns>组件</returns>
    public static T GetOrAddComponent<T>(this GameObject _go)
        where T : Component
    {
        T com = _go.GetComponent<T>();
        if (com == null)
        {
            com = _go.AddComponent<T>();
        }
        return com;
    }

    /// <summary>
    /// 获得或添加组件
    /// </summary>
    /// <param name="_trans">Transform</param>
    /// <param name="_type">组件类型</param>
    /// <returns>组件</returns>
    public static Component GetOrAddComponent(this Transform _trans,Type _type)
    {
        return _trans.gameObject.GetOrAddComponent(_type);
    }

    /// <summary>
    /// 获得或添加组件
    /// </summary>
    /// <param name="_go">GameObject</param>
    /// <param name="_type">组件类型</param>
    /// <returns>组件</returns>
    public static Component GetOrAddComponent(this GameObject _go, Type _type)
    {
        Component com = _go.GetComponent(_type);
        if (com == null)
        {
            com = _go.AddComponent(_type);
        }
        return com;
    }
}
